import request from '@/utils/request'

// 创建项目
export function addProject(data) {
  return request({
    url: '/project/addProject',
    method: 'post',
    params: {
      projName: data.projName,
      projKey: data.projKey
    }
  })
}


// 获取项目详情
export function getProjectDetail(id) {
  return request({
    url: `/project/detail/${id}`,
    method: 'get'
  })
}

// 获取项目列表
export function getProjectList(projKey, projName) {
  return request({
    url: '/project',
    method: 'get',
    params: {
      ...(projKey && { projKey }),
      ...(projName && { projName })
    }
  })
}

// // 修改项目名称
// export function updateProjectInfo(data) {
//   return request({
//     url: '/project/updateProjectInfo',
//     method: 'post',
//     params: {
//       id: data.id,
//       name: data.name
//     }
//   })
// }
// 修改项目名称
export function updateProjectInfo(data) {
  return request({
    url: `/project/updateProjectInfo?id=${data.id}&name=${data.projName}`,
    method: 'post'
  })
}

// 删除项目
export function deleteProject(id) {
  return request({
    url: '/project',
    method: 'delete',
    params: { id }
  })
}

// 获取项目中文件的统计信息
export function getProjectFileStats(projectId) {
  return request({
    url: `/file/project-file-stats`,
    method: 'get',
    params: { projectId }
  })
}





// 文件上传通用接口
export function uploadFile(projKey, fileType, files) {
  const formData = new FormData();
  files.forEach(file => formData.append('files', file));
  formData.append('projKey', projKey);
  formData.append('fileType', fileType);

  return request({
    url: '/file/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 文件下载通用接口
export function downloadFile(projKey, fileType, filePath, versionHash) {
  return request({
    url: '/file/download',
    method: 'post',
    params: {
      projKey,
      fileType,
      filePath,
      versionHash
    },
    responseType: 'blob'
  })
}

// 获取数据版本信息
export function getDataVersions(dataKeys) {
  return request({
    url: '/file/data-version',
    method: 'get',
    params: {
      dataKeys: dataKeys.join(',')
    }
  })
}

// 高层需求模型上传接口
export function uploadHlrModel(projKey, files) {
  return uploadFile(projKey, 'HLR_MODEL', files);
}
// 一致性检查报告上传接口
export function uploadHlrConsistency(projKey, files) {
  return uploadFile(projKey, 'HLR_CONSISTENCY', files);
}
// 架构模型上传接口
export function uploadArchiModel(projKey, files) {
  return uploadFile(projKey, 'ARCHI_MODEL', files);
}

//

export function downloadHlrModel(projKey, filePath, versionHash) {
  return downloadFile(projKey, 'HLR_MODEL', filePath, versionHash);
}

export function downloadControlCode(projKey, filePath, versionHash) {
  return downloadFile(projKey, 'CONTROL_CODE', filePath, versionHash);
}

export function downloadDisplayCode(projKey, filePath, versionHash) {
  return downloadFile(projKey, 'DISPLAY_CODE', filePath, versionHash);
}

export function downloadImplCode(projKey, filePath, versionHash) {
  return downloadFile(projKey, 'IMPL_CODE', filePath, versionHash);
}
